package fairygui.editor
{
   import fairygui.editor.utils.RuntimeErrorUtil;
   import flash.desktop.NativeApplication;
   import flash.events.Event;
   import flash.events.EventDispatcher;
   import flash.events.IOErrorEvent;
   import flash.net.URLLoader;
   import flash.net.URLRequest;
   import flash.net.URLRequestMethod;
   import flash.net.URLVariables;
   import flash.system.Capabilities;
   
   public class VersionCheck extends EventDispatcher
   {
      
      private static var _inst:VersionCheck;
       
      
      private var iLoader:URLLoader;
      
      private var iRunning:Boolean;
      
      public var initiator:EditorWindow;
      
      public const VER_URL:String = "http://jk.fairygui.com/version/check";
      
      public function VersionCheck()
      {
         super();
      }
      
      public static function get inst() : VersionCheck
      {
         if(!_inst)
         {
            _inst = new VersionCheck();
         }
         return _inst;
      }
      
      public function start(param1:EditorWindow = null) : void
      {
         if(this.iRunning)
         {
            return;
         }
         this.initiator = param1;
         this.iRunning = true;
         var _loc2_:XML = NativeApplication.nativeApplication.applicationDescriptor;
         var _loc3_:Namespace = _loc2_.namespace("");
         var _loc4_:String = _loc2_._loc3_::versionNumber;
         var _loc5_:Array = _loc4_.split(".");
         var _loc6_:int = int(_loc5_[0]) * 10000 + int(_loc5_[1]) * 100 + int(_loc5_[2]);
         var _loc7_:URLVariables = new URLVariables();
         _loc7_.pid = _loc2_._loc3_::id;
         _loc7_.ver = _loc6_;
         _loc7_.type = Capabilities.os.toLowerCase().indexOf("mac") != -1?"mac":"win";
         this.iLoader = new URLLoader();
         this.iLoader.addEventListener(Event.COMPLETE,this.checkCompleteHandler);
         this.iLoader.addEventListener(IOErrorEvent.IO_ERROR,this.checkErrorHandler);
         var _loc8_:URLRequest = new URLRequest();
         _loc8_.url = this.VER_URL;
         _loc8_.method = URLRequestMethod.GET;
         _loc8_.data = _loc7_;
         this.iLoader.load(_loc8_);
      }
      
      public function cancel() : void
      {
         this.cleanup();
      }
      
      private function success(param1:String) : void
      {
         var result:Object = null;
         var data:String = param1;
         this.cleanup();
         if(!data)
         {
            FairyGUIEditor.onVersionCheckComplete(null);
            return;
         }
         try
         {
            result = JSON.parse(data);
         }
         catch(err:*)
         {
            result = {};
            result.code = 10001;
            result.msg = RuntimeErrorUtil.toString(err);
         }
         if(result.code == 999)
         {
            NativeApplication.nativeApplication.exit();
            return;
         }
         if(result.code || !result.url)
         {
            return;
         }
         FairyGUIEditor.onVersionCheckComplete(result);
      }
      
      private function failed(param1:String) : void
      {
         this.cleanup();
      }
      
      private function cleanup() : void
      {
         try
         {
            this.iLoader.close();
         }
         catch(e:*)
         {
         }
         this.iLoader = null;
         this.iRunning = false;
      }
      
      private function checkCompleteHandler(param1:Event) : void
      {
         var _loc2_:Object = param1.currentTarget;
         if(_loc2_ != this.iLoader)
         {
            return;
         }
         this.success(_loc2_.data);
      }
      
      private function checkErrorHandler(param1:IOErrorEvent) : void
      {
         var _loc2_:Object = param1.currentTarget;
         if(_loc2_ != this.iLoader)
         {
            return;
         }
         this.failed(param1.text);
      }
   }
}
